package javazuoye.two;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/06/27 08:47
 */
public class _4_2_2 {
    public static void main(String[] args) {
        System.out.println(DP("1235412321232123132441", "1232123212321"));
    }

    public static String DP(String str1, String str2) {
        if (str1.length() > str2.length()) {
            String temp = str1;
            str1 = str2;
            str2 = temp;
        }
        int[][] nums = new int[str1.length() + 1][str2.length() + 1];
        int[] max = new int[]{0, 0};//max,index
        for (int i = 1; i < str1.length() + 1; i++) {
            for (int j = 1; j < str2.length() + 1; j++) {
                if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
                    nums[i][j] = nums[i - 1][j - 1] + 1;
                    if (nums[i][j] > max[0]) {
                        max[0] = nums[i][j];
                        max[1] = j - 1;
                    }
                }
            }
        }
        return str2.substring(max[1] - max[0] + 1, max[1] + 1);
    }
}
